import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ["/", "/login", "/404/404","/liveroom","/d3/pdf"] 




let loacRouterArr = ["/Dashboard/Dashboard","/user/user","/order/order","/self/self","/d3/d3","/permission/menuone","/permission/menutwo","/Video/Live"]



let target = []
router.beforeEach((to,from,next)=>{
  if(to.path == '/login'){
    next()
  }else{
    if(!JSON.parse(window.localStorage.getItem('user')) ){
      next('/login')
    }else{
      if(whiteList.indexOf(to.path)>-1 || JSON.parse(window.localStorage.getItem('user')).username == 'admin'){
        next()
      }else{
        target = []
        let apiRouterArr = JSON.parse(window.localStorage.getItem('Menutree')) 
        console.log(apiRouterArr,'后台获取')
        loacRouterArr.forEach(el=>{
          apiRouterArr.forEach(item=>{
            if(item === el && to.path === item){
              target.length = 1
            }
          })
        })
        target.length==1?next():next('/404/404')
      }
    }
  }
})
// router.beforeEach(async(to, from, next) => {
//   NProgress.start()

//   document.title = getPageTitle(to.meta.title)

//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {

//     if (whiteList.indexOf(to.path) !== -1) {
//       next()
//     } else {
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

router.afterEach(() => {
  NProgress.done()
})
